package com.sc.util.data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Random;


public class DateUtil {
	 /** 
     * 得到几分钟后的时间 
     * @param d 
     * @param day 
     * @return 
     */  
    public static Date getDateMinuteAfter(Date d, int Minute) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + Minute);  
        return now.getTime();  
    }
	/** 
     * 得到几小时前的时间 
     * @param d 
     * @param day 
     * @return 
     */  
    public static Date getDateHourBefore(Date d, int hour) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.HOUR, now.get(Calendar.HOUR) - hour);  
        return now.getTime();  
    }    
	 /** 
     * 得到几天小时后的时间 
     * @param d 
     * @param day 
     * @return 
     */  
    public static Date getDateHourAfter(Date d, int hour) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);  
        return now.getTime();  
    }
	/** 
     * 得到几天前的时间 
     * @param d 
     * @param day 
     * @return 
     */  
    public static Date getDateBefore(Date d, int day) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);  
        return now.getTime();  
    }    
	 /** 
     * 得到几天后的时间 
     * @param d 
     * @param day 
     * @return 
     */  
    public static Date getDateAfter(Date d, int day) {  
        Calendar now = Calendar.getInstance();  
        now.setTime(d);  
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);  
        return now.getTime();  
    }
	/**
	 * 格式化时间年月日时分秒
	 * @param date
	 * @return Date
	 */
	public static Date formatDateYMDHMS(Date date){
		try {
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return df.parse(df.format(date));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 格式化时间年月日时分秒
	 * @param date
	 * @return Date
	 */
	public static Date formatDateYMDHMSStrToDate(String dateStr){
		try {
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return df.parse(dateStr);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 格式化时间年月日
	 * @param date
	 * @return Date
	 */
	public static Date formatDateYMD(Date date){
		try {
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			return df.parse(df.format(date));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 
	 * @Title: formatYMDHMSToString
	 * @Description: 格式化年月日时分秒为string
	 * @author zhangzhe
	 * @date 2016年9月23日下午4:02:22
	 * @param date
	 * @return
	 * @return String 
	 * @throws
	 */
	public static String formatYMDHMSToString(Date date) {
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	
	/**
	 * 
	 * @Title: formatYMDToString
	 * @Description: 格式化年月日为string
	 * @author zhangzhe
	 * @date 2016年9月23日下午4:02:22
	 * @param date
	 * @return
	 * @return String 
	 * @throws
	 */
	public static String formatYMDToString(Date date) {
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	
	/**
	 * 格式化年月日
	 * @param date
	 * @return
	 */
	public static Date formatyMd(String date){
		try {
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 比较分钟差
	 * @param newtime
	 * @param oldtime
	 * @return
	 * @throws ParseException
	 */
	public static long msub(Date newtime,Date oldtime){
		try {
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String nowtime=df.format(newtime);
			String endtime=df.format(oldtime);
			long result=0; 
			result=(df.parse(nowtime).getTime()-df.parse(endtime).getTime())/60000;
			return result;
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public static String formateForUpload(Date date){
		SimpleDateFormat df=new SimpleDateFormat("MMddHHmmss");
		return df.format(date);
	}
	//获取时间戳
	public static String getLontTime(){
		long time=new Date().getTime();
		return time+"";
	}
	
    public static String getOutTradeNo() {
        SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss",
                Locale.getDefault());
        Date date = new Date();
        String key = format.format(date);

        Random r = new Random();
        key = key + r.nextInt();
        key = key.substring(0, 15);
        return key;
    }
	/**
	 * @Title: getOldByWeek
	 * @Description: 计算k周之前的日期
	 * @author zhangzhe
	 * @date 2016年9月23日下午6:42:37
	 * @param week
	 * @return
	 * @return Date 
	 * @throws
	 */
    public static Date getOldByWeek(int week){
    	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");   
    	Date now=new Date();
    	Date old=new Date(now.getTime()-week*7*24*60*60*1000L);//注意类型转化
    	System.out.println(old.getTime());
    	System.out.println("now:"+df.format(now));
    	System.out.println("old:"+df.format(old));
    	return old;
    }
	public static String dateToString(Date date){
		SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(date);
	}
	public static String dateToString1(Date date){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	public static String dateToString2(Date date){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	public static String dateToString2(String date){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	public static Date dateStrToDate(String dateStr){
		SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
		Date d = null;
		try {
			d = df.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return  d;
	}
	public static Date dateStrToYMDHMSDate(String dateStr){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		try {
			d = df.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return  d;
	}
	
	/**
	 * 获取一周前的当前日期
	 * @return
	 */
	public static String getWeekTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateTime = "";
		Calendar c = new GregorianCalendar();
        java.util.Date date = new java.util.Date();
        c.setTime(date);
        c.add(Calendar.SECOND, -7*24*3600);
        java.util.Date dateBefore = c.getTime();//两种把Calendar转化成Long类型的方法（毫秒）
        long nowDate= dateBefore.getTime();//把时间换算成毫秒
        dateTime = sdf.format(nowDate);
        return dateTime;
	}
	
	//获取前月的最后一天
	public static String getFirstMonth(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();   
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime());
//	        System.out.println("===============first:"+first);
        return first;
	}
	
	//获取当前月最后一天
	public static String getLastMonth(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();   
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); 
        String last = format.format(ca.getTime());
//	        System.out.println("===============last:"+last);
        return last;
	}
	
	/***
	 * //获取上周一
	 * @return
	 */
	public static String getLastMonday(){
		Calendar cal = Calendar.getInstance();
		  //n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
		int n = -1;
		String monday;
		cal.add(Calendar.DATE, n*7);
		//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
		cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}
	/***
	 * 获取上周日
	 * @return
	 */
	public static String getLastSunday(){
		Calendar cal = Calendar.getInstance();
		  //n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
		int n = 0;
		String monday;
		cal.add(Calendar.DATE, n);
		//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
		cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}

	public static void main(String[] args) {
	//	System.out.print(BigDecimal.valueOf(Double.parseDouble("0.011")).multiply(BigDecimal.valueOf(100))
	//			.setScale(0, BigDecimal.ROUND_HALF_UP));
		
	//	System.out.println("wx20170316171233013d3ce87e0314954010".indexOf("wx"));
		/*Date d =dateStrToYMDHMSDate("2017-04-17 17:02:08");
		
		Date nowTime = new Date();
	    Date pro_start = dateStrToYMDHMSDate("2017-04-17 17:02:08"); //促销开始时间
	    Date pro_end  =dateStrToYMDHMSDate("2017-04-21 17:02:08"); //促销结束时间
		System.out.println("nowTime TimeStamp: " + nowTime.getTime()
				+"\n"
				+"pro_start TimeStamp: " + pro_start.getTime()
				+"\n"
				+"pro_end TimeStamp: " + pro_end.getTime());*/
		
		
		/*String s = formatYMDToString(getDateBefore(new Date(), 14));
		System.out.println(s);
		
		String ss = getWeekTime();
		System.out.println(ss);*/
		
		System.out.println(getLastMonday());
		System.out.println(getLastSunday());
		
		System.out.println(formatyMd(getLastMonday()).getTime());
		
		
		/*BigDecimal d = new BigDecimal("94.579");
		System.out.println(d.setScale(2,BigDecimal.ROUND_HALF_UP).toString());*/
	}
}	
